/**
 * Created by zy on 2016/9/28.
 * 整改效果 - 查看整改记录 - 阴保整改 - 维修整改 - 腐蚀控制
 */


;(function ($, window, document, undefined) {

    // 整改记录编号
    var rid = $('#JQ_repairTaskId').val();

    // 管道
    var $pipe = $('#JQ_pipe');
    // 起点位置
    var $begin = $('#JQ_begin');
    // 终点位置
    var $end = $('#JQ_end');
    // 检测日期
    var $check_date = $('#JQ_check_date');
    // 整改后的检测日期
    var $checked_date = $('#JQ_checked_date');
    // 项目名称
    var $project_name = $('#JQ_project_name');


    if (rid) {
        DyyBase.ajax(corrctrlApi.url_cath_details(), {repairTaskId: rid}, function (dataset) {
            if(dataset.bool){
                $pipe.val(dataset.result.repairTask.pipeLineName);
                $begin.val(dataset.result.repairTask.fromPointName);
                $end.val(dataset.result.repairTask.toPointName);
                $check_date.val(Dyy.getDate(dataset.result.repairTask.planStartDate));
                $checked_date.val(Dyy.getDate(dataset.result.repairTask.planCompletionDate));
                $project_name.val(dataset.result.repairTask.name);
            }
        });
    }

    // 绘制图表
    $('#JQ_vs_effect').click(function(){

        var v1 = $('#JQ_check_date').val();
        var v2 = $.trim($('#JQ_checked_date').val());

        if(v1){
            if(v2){
                if(!(Dyy.checkDateLate(v1,v2))){
                    $('#JQ_checked_date').TIP({content:'整改后日期应晚于整改前日期!'});
                    return;
                }
            }else{
                $('#JQ_checked_date').TIP({content:'请选择整改后的检测日期!'});
                return;
            }
        }

        var temp = [
            {EQPT_NUMBER: 'A1', ON_POTENTIAL_AVG1: 1000, OFF_POTENTIAL_AVG1: 900, FREE_CORROSION_POTENTIAL1: -1000, ON_POTENTIAL_AVG2: 13, OFF_POTENTIAL_AVG2: 1000, FREE_CORROSION_POTENTIAL2: 31},
            {EQPT_NUMBER: 'A2', ON_POTENTIAL_AVG1: 1200, OFF_POTENTIAL_AVG1: 1000, FREE_CORROSION_POTENTIAL1: -950, ON_POTENTIAL_AVG2: 16, OFF_POTENTIAL_AVG2: 1100, FREE_CORROSION_POTENTIAL2: 33},
            {EQPT_NUMBER: 'A3', ON_POTENTIAL_AVG1: 1250, OFF_POTENTIAL_AVG1: 956, FREE_CORROSION_POTENTIAL1: -990, ON_POTENTIAL_AVG2: 14, OFF_POTENTIAL_AVG2: 1200, FREE_CORROSION_POTENTIAL2: 30},
            {EQPT_NUMBER: 'A4', ON_POTENTIAL_AVG1: 1350, OFF_POTENTIAL_AVG1: 1100, FREE_CORROSION_POTENTIAL1: -890, ON_POTENTIAL_AVG2: 15, OFF_POTENTIAL_AVG2: 1021, FREE_CORROSION_POTENTIAL2: 34},
            {EQPT_NUMBER: 'A5', ON_POTENTIAL_AVG1: 1450, OFF_POTENTIAL_AVG1: 980, FREE_CORROSION_POTENTIAL1: -1200, ON_POTENTIAL_AVG2: 13, OFF_POTENTIAL_AVG2: 1150, FREE_CORROSION_POTENTIAL2: 32},
            {EQPT_NUMBER: 'A6', ON_POTENTIAL_AVG1: 1630, OFF_POTENTIAL_AVG1: 890, FREE_CORROSION_POTENTIAL1: -1300, ON_POTENTIAL_AVG2: 15, OFF_POTENTIAL_AVG2: 1250, FREE_CORROSION_POTENTIAL2: 35}
        ];

        var dataset = [
            [
                {x: 1, y: 2},
                {x: 2, y: 7},
                {x: 3, y: 3},
                {x: 4, y: 4},
                {x: 5, y: 9},
                {x: 6, y: 4},
                {x: 7, y: 3},
                {x: 8, y: 2}
            ],
            [
                {x: 1, y: 11},
                {x: 2, y: 15},
                {x: 3, y: 10},
                {x: 4, y: 14},
                {x: 5, y: 16},
                {x: 6, y: 19},
                {x: 7, y: 12},
                {x: 8, y: 13}
            ],
            [
                {x: 1, y: 20},
                {x: 2, y: 27},
                {x: 3, y: 21},
                {x: 4, y: 26},
                {x: 5, y: 22},
                {x: 6, y: 25},
                {x: 7, y: 21},
                {x: 8, y: 28}
            ]
        ];

        //console.log(d3Charts.axisData(dataset));

        var chk_date = $.trim($checked_date.val());
        if(chk_date){
            DyyBase.ajax(corrctrlApi.url_cath_effect_charts(),{repairTaskId:rid,testingDate:chk_date},function(dataset){
                if(dataset.bool){
                    if(dataset.result){
                        if(dataset.result.length > 0){
                            // 通电电位 - 整改前
                            var ON_POTENTIAL_AVG1 = d3Charts.formart(dataset.result,{x:'EQPT_NUMBER',y:'ON_POTENTIAL_AVG1'});
                            // 断电电位 - 整改前
                            var OFF_POTENTIAL_AVG1 = d3Charts.formart(dataset.result,{x:'EQPT_NUMBER',y:'OFF_POTENTIAL_AVG1'});
                            // 自然电位 - 整改前
                            var FREE_CORROSION_POTENTIAL1 = d3Charts.formart(dataset.result,{x:'EQPT_NUMBER',y:'FREE_CORROSION_POTENTIAL1'});

                            // 通电电位 - 整改后
                            var ON_POTENTIAL_AVG2 = d3Charts.formart(dataset.result,{x:'EQPT_NUMBER',y:'ON_POTENTIAL_AVG2'});
                            // 断电电位 - 整改后
                            var OFF_POTENTIAL_AVG2 = d3Charts.formart(dataset.result,{x:'EQPT_NUMBER',y:'OFF_POTENTIAL_AVG2'});
                            // 自然电位 - 整改后
                            var FREE_CORROSION_POTENTIAL2 = d3Charts.formart(dataset.result,{x:'EQPT_NUMBER',y:'FREE_CORROSION_POTENTIAL2'});

                            var alldata = [ON_POTENTIAL_AVG1,OFF_POTENTIAL_AVG2,OFF_POTENTIAL_AVG1,FREE_CORROSION_POTENTIAL1];

                            // 画图表
                            d3Charts.ordinal({
                                container: 'JQ_dy_charts',
                                data: alldata,
                                color: ['#f36e4b','#aaaaaa', '#2abf63', '#409ee8'],
                                tip: {
                                    show: true,
                                    xtag: '测试桩：',
                                    ytag: ['通电电位：', '断电电位(整改后)：', '断电电位(整改前)：', '自然电位：']
                                },
                                xAxis: {
                                    ticks: Infinity
                                },
                                yAxis: {
                                    ticks: Infinity
                                },
                                layout: {
                                    xtag: '测试桩',
                                    ytag: '电位值(mV)',
                                    margin: {
                                        left: 40,
                                        right: 30,
                                        top: 30,
                                        bottom: 40
                                    },
                                    ygrid: true
                                },
                                areaShow: true,
                                area: [
                                    {
                                        range: [-850],
                                        background: '#ff0000'
                                    },
                                    {
                                        range: [-1200],
                                        background: '#ff0000'
                                    }
                                ]
                            });
                        }else{
                            $('#JQ_dy_charts').trigger('clear');
                        }
                    }
                }
            });
        }else{
            $checked_date.TIP({content: '请选择整改后的检测日期!'});
        }
    });

    // 返回
    $('#JQ_goback_btn').click(function(){
        window.history.go(-1);
    });

    // 导出pdf
    $('#JQ_export_pdf').click(function(){
        var pipeName = $pipe.val();
        var projectName = $project_name.val();
        $(this).EXPT({
            type: 'name',
            name: pipeName + projectName + '阴保整改效果图',
            yesBtnAct: function(callback,fname){
                callback();
                canvasPdf.exportHtmlPdf({
                    dom: $('#JQ_pdf_area'),
                    server: DyyBase.root_path + '/comm/downImgPdf.json',
                    param: {
                        fileName: fname,
                        name: projectName + '阴保整改效果图'
                    }
                });
            }
        });
    });

})(jQuery, window, document);